import { ViewUtil } from "db://app-game/core/utils/ViewUtil";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { MoveModel, MoveToModelComp } from "../../common/ecs/bll/MoveToSystem";
import { BTTreeComp } from "../../common/ecs/model/BTTreeComp";
import { smc } from "../../common/ecs/model/SingletonModuleComp";
import { LittleMonsterLoadParamsType } from "./LittleMonster.type";
import { LittleMonsterModelComp } from "./model/LittleMonsterModelComp";
import { LittleMonsterViewComp } from "./view/LittleMonsterViewComp";

/** 小怪 */
@ecs.register("LittleMonster")
export class LittleMonster extends ecs.Entity {
  // 数据层
  LittleMonsterModel!: LittleMonsterModelComp;

  // 业务层
  BTTree!: BTTreeComp;
  MoveToModel!: MoveToModelComp;

  // 视图层
  LittleMonsterView!: LittleMonsterViewComp;
  init() {
    this.addComponents(LittleMonsterModelComp);
  }

  /** 加载 */
  load(res: LittleMonsterLoadParamsType) {
    const { id, parent, initPos } = res;
    // 1.初始化怪兽数据
    this.LittleMonsterModel.id = id;

    // 2.加载怪兽视图
    const { data } = this.LittleMonsterModel;
    let node = ViewUtil.createPrefabNode(data.prefabPath);
    let mv = node.getComponent(LittleMonsterViewComp);
    this.add(mv);

    node.parent = parent;
    node.setPosition(initPos);

    // 3.添加业务组件
    // this.add(BTTreeComp);
    this.add(MoveToModelComp);

    // 4.行为树赋值
    // this.BTTree.tree = new LittleMonsterBTTree();
    // this.BTTree.behaviorManager = new BehaviorManager(this, this.BTTree.tree);
    // this.BTTree.behaviorManager.start();

    // 6.智能体赋值W
    this.MoveToModel.node = node;
    this.MoveToModel.map = smc.level.map.map;
    this.MoveToModel.speed = data.attribute.speed;
    this.MoveToModel.offset = 1;
    this.MoveToModel.moveModel = MoveModel.FlowGridMove;
    this.MoveToModel.simulator = smc.level.simulator;

    this.MoveToModel.flowGrid = smc.level.map.MapRenderModel.flowGrid;
  }
}
